#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlError>
#include <QSqlQuery>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QSqlDatabase db =QSqlDatabase::addDatabase("QMYSQL");
    QStringList list=QSqlDatabase::drivers();
    qDebug() <<list;
    db.setHostName("127.0.0.1");
    db.setDatabaseName("testdb");
    db.setUserName("root");
    db.setPassword("root");
    if(!db.open()){
        qDebug() << "error:"  << db.lastError().text();
    }else{
        qDebug()<<"连接成功！";
    }

    QSqlQuery query;
    QString sql="select * from person";
    query.exec(sql);
    while(query.next()){
        qDebug()<<query.value(0).toUInt()
                 <<query.value(1).toUInt()
                 <<query.value(2).toString()
                 <<query.value(3).toString();
    }
    db.close();
}

MainWindow::~MainWindow()
{
    delete ui;
}
